package com.sinven.service;


import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sinven.mapper.UserMapper;
import com.sinven.pojo.Article;
import com.sinven.pojo.User;
import com.sinven.util.PageBean;
 
@Service
public class UserServiceImpl implements UserService {
	//这个注解就是spring可以自动帮你把bean里面引用的对象的setter/getter方法省略
	@Autowired
	private UserMapper userMapper;
	//重写
	@Override
	/**
	 * 注册用户
	 */
	public boolean AddUser(User user) throws Exception {
		int count = userMapper.AddUser(user);
		if (count > 0) {
			return true;
		}
		return false;
	}

	@Override
	/**
	 * 管理员删除用户
	 */
	public boolean DeleteUser(int u_id) throws Exception {
		int count = userMapper.DeleteUser(u_id);
		if (count > 0) {
			return true;
		}
		return false;
	}

	@Override
	/**
	 * 修改个人信息
	 */
	public boolean UpdateUser(User user) throws Exception {
		int count = userMapper.UpdateUser(user);
		if (count > 0) {
			return true;
		}
		return false;
	}
	
	@Override
	/**
	 * 管理员修改个人信息
	 */
	public boolean HTUpdateUser(User user) throws Exception {
		int count = userMapper.HTUpdateUser(user);
		if (count > 0) {
			return true;
		}
		return false;
	}


	@Override
	/**
	 * 管理员修改lock
	 */
	public boolean UpdateUserLock(Date date) throws Exception {
		int count = userMapper.UpdateUserLock(date);
		if (count > 0) {
			return true;
		}
		return false;
	}

	@Override
	/**
	 * 登陆
	 */
	public User login(User user) throws Exception {

		
		try {
			User user2 = userMapper.login(user);
			if (user2 == null) {
				throw new Exception("该用户不存在");
			}
			
			if (!user2.getU_password().equals(user.getU_password())) {
				throw new Exception("登陆成功");
			}
			user2.setU_lastlogin(new Date());
			
			userMapper.UpdateUser(user2);

			return user2;
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	/**
	 * 查询普通用户数
	 */
	public int CountUser() throws Exception {
		int count = userMapper.CountUser();
		return count;
	}

	@Override
	public User SelectUserById(int u_id) throws Exception {
		User selectUserById = userMapper.SelectUserById(u_id);
		if(selectUserById==null) {
			System.out.println("没有找到该用户"); 
		}
		return selectUserById;
	}

	@Override
	public PageBean<User> SelectAll(int pageNum,int pageSize) throws Exception {
		List<User> selectAll = userMapper.SelectAll();
		int totalRecord=selectAll.size();
		System.out.println(totalRecord);
		PageBean<User> pb=new PageBean<User>(pageNum, pageSize, totalRecord);
		int startIndex=pb.getStartIndex();
		List<User> datas = userMapper.SelectAllByFenYe(startIndex, pageSize);
		pb.setListUser(datas);
		return pb;
	}
	@Override
	public PageBean<User> SelectAllByType(String u_type,int pageNum,int pageSize) throws Exception {
		List<User> selectAll = userMapper.SelectAllByType(u_type);
		int totalRecord=selectAll.size();
		System.out.println(totalRecord);
		PageBean<User> pb=new PageBean<User>(pageNum, pageSize, totalRecord);
		int startIndex=pb.getStartIndex();
		List<User> datas = userMapper.SelectAllByTypeAndFenYe(u_type, startIndex, pageSize);
		pb.setListUser(datas);
		return pb;
	}

	@Override
	public boolean SelectByUname(String u_name) throws Exception {
		long res=userMapper.SelectByUname(u_name);
		if(res>0) {
			System.out.println("该用户已存在！");
			return true;
		}
		return false;
	}

	@Override
	public boolean UpdateUserState(int u_id, int u_state) throws Exception {
		int updateUserState = userMapper.UpdateUserState(u_id, u_state);
		if(updateUserState>0) {
			System.out.println("修改成功！");
			return true;
		}
		return false;
	}

}
